<?
$urltype = $type;
$page = 0;
$searchTrem = '';
$sortBy = 'date';
$order = 'desc';
$view = 'show';
if (isset($seachval_type))
    $urltype = $seachval_type;

//cotroller/function/sortby/order/searchteam/page
if ($this->uri->segment(7) != FALSE) {
    $searchTrem = urldecode($this->uri->segment(6));
    $page = $this->uri->segment(7);
    $sortBy = $this->uri->segment(4);
    $order = $this->uri->segment(5);
    $view = $this->uri->segment(3);
} else if ($this->uri->segment(6) != FALSE) {
    $page = $this->uri->segment(6);
    $sortBy = $this->uri->segment(4);
    $order = $this->uri->segment(5);
    $view = $this->uri->segment(3);
} else if ($this->uri->segment(5) != FALSE) {
    $page = 0;
    $sortBy = $this->uri->segment(4);
    $order = $this->uri->segment(5);
    $view = $this->uri->segment(3);
}

$status = $this->uri->segment(2);
$searchArray = unserialize($searchTrem);
$loadUrl = base_url() . 'index.php/property/' . $view . '/' . $urltype . '/' . $page;
$loadUrlGallery = base_url() . 'index.php/property/gallery/' . $urltype . '/';
$loadUrlShow = base_url() . 'index.php/property/show/' . $urltype . '/';
echo '<form id="intList">';
if (count($searchArray) > 0 && is_array($searchArray)) {
   
    foreach ($searchArray as $saname => $sa) {        
        echo '<input type="hidden" name="' . $saname . '" value="' . $sa . '" />';
        ?>         
        <script type="text/javascript">
            $('#<?= $saname ?>').val('<?= $sa ?>'); 
        </script>
        <?php
    }
    
}else{
    if(isset($search_minprice) && trim($search_minprice)!=''){
        echo '<input type="hidden" name="search_minprice" value="' . $search_minprice . '" />';
        ?>         
        <script type="text/javascript">
            $('#search_minprice').val('<?= $search_minprice ?>'); 
        </script>
        <?php
    }
    if(isset($search_maxprice) && trim($search_maxprice)!=''){
        echo '<input type="hidden" name="search_maxprice" value="' . $search_maxprice . '" />';
        ?>         
        <script type="text/javascript">
            $('#search_maxprice').val('<?= $search_maxprice ?>'); 
        </script>
        <?php
    }
    if(isset($search_bathroom) && trim($search_bathroom)!=''){
        echo '<input type="hidden" name="search_bathroom" value="' . $search_bathroom . '" />';
        ?>         
        <script type="text/javascript">
            $('#search_bathroom').val('<?= $search_bathroom ?>'); 
        </script>
        <?php
    }
    if(isset($search_bedroom) && trim($search_bedroom)!=''){
        echo '<input type="hidden" name="search_bedroom" value="' . $search_bedroom . '" />';
        ?>         
        <script type="text/javascript">
            $('#search_bedroom').val('<?= $search_bedroom ?>'); 
        </script>
        <?php
    }
    if(isset($search_propertytype) && trim($search_propertytype)!=''){
        echo '<input type="hidden" name="search_propertytype" value="' . $search_propertytype . '" />';  
        ?>         
        <script type="text/javascript">
            $('#search_propertytype').val('<?= $search_propertytype ?>'); 
        </script>
        <?php
    }
     if(isset($suburbs_id) && trim($suburbs_id)!=''){
        echo '<input type="hidden" name="suburbs_id" value="' . $suburbs_id . '" />';        
        ?>         
        <script type="text/javascript">
            $('#suburbs_id').val('<?= $suburbs_id ?>'); 
        </script>
        <?php
    }
     if(isset($surrounding) && trim($surrounding)!=''){
        echo '<input type="hidden" name="surrounding" value="' . $surrounding . '" />';        
    }
     if(isset($search_status) && trim($search_status)!=''){
          echo '<input type="hidden" name="search_status" value="' . $search_status . '" />';
          ?>         
        <script type="text/javascript">
            $('#search_status').val('<?= $search_status ?>'); 
        </script>
        <?php
    }if(isset($suburbs) && trim($suburbs)!=''){
          echo '<input type="hidden" name="suburbs" value="' . $suburbs . '" />';
          ?>         
        <script type="text/javascript">
            $('#suburbs').val('<?= $suburbs ?>'); 
        </script>
        <?php
    }
    
}
echo '</form>';
?>
<script type="text/javascript">
    
    $(document).ready(function(){  
        autocomplate('suburbs');
        //initial load  
        dipalyStyles('<?= $sortBy ?>','<?= $order ?>');
        var intfields = $("#intList :input").serializeArray();
        loadProperties('<?= $loadUrl ?>','<?= $sortBy ?>','<?= $order ?>',intfields,'<?= $page ?>');  
        
        $('#seachvals :input').keypress(function(e) {
            if(e.which == 13) {
                jQuery(this).blur();
                searchListing();
            }
        });
        
        $('#suburbs').keypress(function(e) {
            if(e.which == 13) {
                e.preventDefault();
                jQuery(this).blur();
                searchListing();
            }
        });
        
        $( "#suburbs" ).autocomplete({
            delay: 100,
            source: function(request, response) {
                $.ajax({
                    url: "<?php echo site_url('home/ajax_search?part=') ?>"+request.term,
                    dataType: 'json',
                    success: function(json) {		
                        response($.map(json, function(item,key) {
                            return {
                                label: item,
                                values: key
                            }
                        }));
                        return false
                    }
                });
            }, 
            select: function(event, ui) {
                $( "#suburbs_id" ).val(ui.item.values);
            },
            focus: function(event, ui) {
                return false;
            }
        });
        
    });
    
   
    
    function searchListing(){
        var sortby= $('#sortBy').val();
        var order= $('#orderBy').val(); 
        var fields = $("#seachvals :input").serializeArray();       
        loadProperties('<?= $loadUrl ?>',sortby,order,fields,'0')
    }
    
    function openProperty(id) {
        window.location = "<?= site_url('property/profile/' . $type) ?>/"+id;
    }
</script>
<script>
    function loadProperties(url,sortby,order,searchby,page){
       
        $.ajax({  
            type:"post",
            data: {searchby: searchby,sortby:sortby,order:order,page:page},
            url:url,
            progress:function(data){
                        $('#datalist-navigation').html('searching...');
                    },
            success: function(data){
                var obj = jQuery.parseJSON(data);                                      
                $('#properties').html(obj.tasks);
                $("#datalist-navigation").html(obj.links);
                $("#Count").html(obj.total);
                $("#datalist-navigation").show();   
               
               
                if(obj.search!=0){
                    var searchArray = obj.search;
                    if(obj.search.search_propertytype!=0)
                        $('#search_propertytype').val(obj.search.search_propertytype);
                    else
                        $('#search_propertytype').val('');
                     
                    if(obj.search.search_minprice!=0)
                        $('#search_minprice').val(obj.search.search_minprice);
                    else
                        $('#search_minprice').val('');
                    
                    if(obj.search.search_maxprice!=0)
                        $('#search_maxprice').val(obj.search.search_maxprice);
                    else
                        $('#search_maxprice').val('');
                     
                    if(obj.search.search_bathroom!=0)
                        $('#search_bathroom').val(obj.search.search_bathroom);
                    else
                        $('#search_bathroom').val('');
                     
                    if(obj.search.search_bedroom!=0)
                        $('#search_bedroom').val(obj.search.search_bedroom);
                    else
                        $('#search_bedroom').val('');
                     
                   
                    if(obj.search.suburbs!=0)
                        $('#suburbs').val(obj.search.suburbs);
                    else
                        $('#suburbs').val('');
                    
                    if(obj.search.suburbs_id!=0)
                        $('#suburbs_id').val(obj.suburbs_id.suburbs);
                    else
                        $('#suburbs_id').val('');
                     
                    if(obj.search.search_status!=0)
                        $('#search_status').val(obj.search.search_status);
                    else
                        $('#search_status').val('');
                    
                }
                
                if(obj.sortby!=0)
                    $('#datalist-sort-order').val(obj.sortby);
                
                if(obj.order=='asc')
                    $('#datalist-sort-asc').prop('checked', true);
                
                
            } 
        });  
    }
    
    function sortbyVal(sortbyVal, orderVal){
        var fields = $("#seachvals :input").serializeArray();  
        var sortby= sortbyVal;
        var order= orderVal;
        
        //chage attribute select
        dipalyStyles(sortby,order);
        
       
        loadProperties('<?= $loadUrl ?>', sortby,order,fields,'<?= $page ?>');
        
    }
    
    function dipalyStyles(sortby,order){
        $('#dateFilterdesc').attr('class', 'order');
        $('#priceFilterdesc').attr('class', 'order');
        $('#priceFilterasc').attr('class', 'order');
        $('#inspection_dateFilterdesc').attr('class', 'order');
        
        $('#'+sortby+'Filter'+order).attr('class', 'order active');
        
        $('#sortBy').val(sortby);
        $('#orderBy').val(order);
    }
    
    function chagngeView(viewname){
        var sortby= $('#sortBy').val();
        var order= $('#orderBy').val(); 
        
        if(viewname=='gallery'){
            $('#view').val('gallery');
            var fields = $("#seachvals :input").serializeArray();    
            loadProperties('<?= $loadUrlGallery ?>0', sortby,order,fields,'0');
            $('#list-view-tab').attr('class', 'selected-list');
            $('#gallery-view-tab').attr('class', 'selected-gallery active');
           
        }
        else{
            $('#view').val('show');
            var fields = $("#seachvals :input").serializeArray();    
            loadProperties('<?= $loadUrlShow ?>0', sortby,order,fields,'0');
            $('#list-view-tab').attr('class', 'selected-list active');
            $('#gallery-view-tab').attr('class', 'selected-gallery');
            
        }
    }

</script>
<div class="content office-page" id="page-body">
    <div id="search-properties-1">

        <h3>Quick Search</h3>
        <form id="seachvals">
            <p>
            </p>

            <table>
                <tr>
                    <td>
                        <div style="margin:6px 0 2px 10px"><input type="checkbox" name="surrounding" id="surrounding" <?=((isset($surrounding)&& $surrounding!=1))?'':'checked="checked"'?> />Surrounding Suburbs</div>
                        <div id="searchform-title" class="clearfix">
                            <div id="searchform-box">
                                <div class="search-image">
                                    <input type="text" 
                                           name="suburbs" 
                                           placeholder="Suburb/Town" 
                                           id ="suburbs" 
                                           data-input-type="autocomplete" 
                                           value="<?= (isset($seachval_suburbs)) ? $seachval_suburbs : '' ?>"
                                           /> 
                                </div>
                                <input type="hidden" id="suburbs_id" value="<?=(isset($suburbs_id) && $suburbs_id!='')?$suburbs_id:'' ?>" name ="suburbs_id">
                                <div id="results-suburbs"></div>
                            </div>

                        </div>
                    </td>
                    <td><label>Minimum Price:</label> 
                        <?= form_dropdown('search_minprice', $Minprice, (isset($search_minprice)) ? $search_minprice : '', 'id="search_minprice" class="sale"'); ?>

                    </td>
                    <td><label>Maximum Price:</label> 
                        <?= form_dropdown('search_maxprice', $Maxprice, (isset($search_maxprice)) ? $search_maxprice : '', 'id="search_maxprice" class="sale column-end"'); ?>
                    </td>
                    <td></td>
                </tr>

                <tr>
                    <td><label>Property Type</label> 

                        <?= form_dropdown('search_propertytype', $propertyType, (isset($search_propertytype)) ? $search_propertytype : '', 'id="search_propertytype"'); ?>
                    </td>
                    <td><label>Bedrooms:</label> 
                        <?= form_dropdown('search_bedroom', $BedroomCount, (isset($search_bedroom)) ? $search_bedroom : '', 'id="search_bedroom" class="last"'); ?> 
                    </td>


                    <td><label>Bathrooms:</label> 
                        <?= form_dropdown('search_bathroom', $BathroomCount, (isset($search_bathroom)) ? $search_bathroom : '', 'id="search_bathroom" class="last"'); ?> 
                    </td>
                    <td>
                        <input type="hidden" name="contoller" id="contoller" value="<?= $type ?>" />
                        <input type="hidden" name="view" id="view" value="<?= (isset($view)) ? $view : 'show' ?>" />
                        <input type="hidden" name="sortBy" id="sortBy" value="<?= (isset($sortBy)) ? $sortBy : 'date' ?>" />
                        <input type="hidden" name="orderBy" id="orderBy" value="<?= (isset($orderBy)) ? $orderBy : 'desc' ?>" />
                        <input type="button" id="submit" class="orange-button listing last" onclick="searchListing()" name="search" value="Search" />
                    </td>
                </tr>
            </table>
        </form>
        <div class="clearer"></div>
    </div>

    <div id="change">
        <aside class="header blog-heading listings">
            <h1><?= $subTitle ?></h1>
        </aside>

        <div class="clearfix" id="tabs">
            <div class="bar"></div>
            <a href="#change" onClick="chagngeView('show')" id="list-view-tab" class="selected-list <?= ($view == 'gallery') ? '' : 'active' ?>">
                <span>
                    <img src="<?php echo base_url(); ?>img/list-view.png" alt="" style="margin: 0px 0px 4px 56px;" />
                </span>List View
            </a> 
            <a href="#change" onClick="chagngeView('gallery')" id="gallery-view-tab" class="selected-gallery <?= ($view == 'gallery') ? 'active' : '' ?>">
                <span>
                    <img src="<?php echo base_url(); ?>img/gallery-view.png" alt=""  style="margin: 0px 0px 1px 56px;" />
                </span>
                Gallery View
            </a>
        </div>
        <div class="clearer"></div>
        <div class="clearfix" id="sort-options">
            <span class="cap gogecko-orange">ORDER BY:</span>
            <ul>
                <li  id="dateFilterdesc" class="active"><a
                        href="#change" onClick="sortbyVal('date','desc')"  >Latest Listings</a>
                </li>
                <li id="priceFilterdesc" ><a href="#change" onClick="sortbyVal('price','desc')" >Highest
                        Price</a>
                </li>
                <li id="priceFilterasc" >  <a href="#change" onClick="sortbyVal('price','asc')" >Lowest
                        Price</a>
                </li>
                <li id="inspection_dateFilterdesc" ><a href="#change" onClick="sortbyVal('inspection_date','desc')"  >Open
                        Homes</a>
                </li>
            </ul>
        </div>
        <div class="clearer"></div>
        <div id="properties"></div>

        <div class="clearfix"></div>
        <div id="datalist-navigation" class="datalist-navigation"></div>
    </div>
</div>
